package com.sec.android.app.myfiles.dialog;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.LockUnlockCmd.LockProgress;
import com.sec.android.app.myfiles.feature.LockUnlockMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

public class ConfirmLockUnlockDialog extends DialogFragment {
    private Context mContext;

    private CheckBox mCheckBox;
    private int mProcessId;
    private AbsMyFilesFragment mFragment;

    public static ConfirmLockUnlockDialog getInstance(Context context, AbsMyFilesFragment fragment) {
        ConfirmLockUnlockDialog ret = null;

        if (!LockUnlockMgr.getInstance().getDoNotShowConfirmDialog(context)) {
            ret = new ConfirmLockUnlockDialog();
            ret.setValue(context, fragment);
        }

        return ret;
    }

    public ConfirmLockUnlockDialog() {

    }

    private void setValue(Context context, AbsMyFilesFragment fragment) {
        mContext = context;
        mFragment = fragment;
        mProcessId = fragment.getProcessId();
    }

    // Because of its in the dialog layout, so the parent view its empty
    @SuppressLint("InflateParams")
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
        LayoutInflater factory = LayoutInflater.from(getActivity());
        View mCustomDialog = factory.inflate(R.layout.custom_dialog_lock, null);
        int okResId;

        dialogBuilder.setView(mCustomDialog);

        mCheckBox = (CheckBox) mCustomDialog.findViewById(R.id.show_checkbox);
        TextView mNotiTxt = (TextView) mCustomDialog.findViewById(R.id.notification);

        TextView textDoNotShow = (TextView) mCustomDialog.findViewById(R.id.do_not_show_textview);
        textDoNotShow.setSoundEffectsEnabled(false);
        textDoNotShow.setOnClickListener(onDoNotShowCheck());

        mNotiTxt.setText(R.string.unlock_notification);
        dialogBuilder.setTitle(R.string.unlock);
        okResId = R.string.unlock;

        dialogBuilder.setPositiveButton(okResId, onOK());
        dialogBuilder.setNegativeButton(R.string.cancel, onCancel());

        dialogBuilder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_MENU)
                    return true;
                else
                    return false;
            }
        });
        dialogBuilder.setCancelable(true);

        return dialogBuilder.create();
    }
    private OnClickListener onDoNotShowCheck() {
        return new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mCheckBox.performClick();
            }
        };
    }

    private DialogInterface.OnClickListener onOK() {
        return new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (dialog != null) {
                    dialog.dismiss();
                }

                if (mCheckBox.isChecked()) {
                    LockUnlockMgr.getInstance().setDoNotShowConfirmDialog(mContext);
                }

                MyFilesFacade.lockUnlockRecord(mProcessId, null, mFragment, null, LockProgress.INPUT_PASSWD);
            }
        };
    }
    private DialogInterface.OnClickListener onCancel() {
        return new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        };
    }

}
